package com.maktoub.apiclientstart.client;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.maktoub.apiclientstart.Utils.SignUtils;
import com.maktoub.apiclientstart.entity.HelloGetListParam;
import com.maktoub.apiclientstart.entity.InputParam;
import com.maktoub.apiclientstart.entity.PostInputParamTest;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 调用接口
 *
 * @author maktoub
 */
@Data
public class ApiInterfaceClient {
//    网关
//    private final String ROOT_PATH="http://8.138.87.142:7001/api/";
    private final String ROOT_PATH="http://localhost:7001/api/";
    @Resource(name = "redisTemplate2")
    RedisTemplate redisTemplate;
    private final String REDIS_RANDOM_NAMESPACE="random:";

    private String accessKey;
    private String secrectKey;

    public ApiInterfaceClient(String accessKey, String secrectKey) {
        this.accessKey = accessKey;
        this.secrectKey = secrectKey;
    }

    public HttpResponse saytest( String testStr) {
        String url=ROOT_PATH+"say/test";
        HashMap<String, String> hashMap = getHashMap(testStr);
        HttpResponse response = HttpRequest.get(url).form("test", testStr)
                .addHeaders(hashMap)
                .execute();
        return response;
    }

    public HttpResponse sayHelloGet( HelloGetListParam helloGetListParam) {
        String url=ROOT_PATH+"say/hello";
        String jsonStr = JSONUtil.toJsonStr(helloGetListParam);
        HashMap<String, String> hashMap = getHashMap(jsonStr);
        HttpResponse response = HttpRequest.get(url).form("name", helloGetListParam.getName())
                .form("age",helloGetListParam.getAge())
                .addHeaders(hashMap)
                .execute();
        return response;
    }


    public HttpResponse sayWelcomeGet(String name) {
        String url=ROOT_PATH+"say/welcome";
        HashMap<String, String> hashMap = getHashMap(name);
        HttpResponse response = HttpRequest.post(url)
                .addHeaders(hashMap)
                .form("name", name)
                .execute();

        return response;
    }

    public HttpResponse sayWelcomePostRestFul(List<InputParam> inputParam) {
        String url=ROOT_PATH+"say/welcome";
        String jsonStr = JSONUtil.toJsonStr(inputParam);
        HashMap<String, String> hashMap = getHashMap(jsonStr);
        HttpResponse response = HttpRequest.post(url)
                .addHeaders(hashMap)
                .body(jsonStr)
                .execute();
        return response;
    }

    public HttpResponse sayWelcomePostOrdinaryForm(String name) {
        String url=ROOT_PATH+"say/welcome";
        HashMap<String, String> hashMap = getHashMap(name);

        HttpResponse response = HttpRequest.post(url)
                .addHeaders(hashMap)
                .form("name", name)
                .execute();
        return response;
    }

    public HttpResponse sayHelloPost( PostInputParamTest postInputParamTest) {
        String url=ROOT_PATH+"say/hello";
        String jsonStr = JSONUtil.toJsonStr(postInputParamTest);
        HashMap<String, String> hashMap = getHashMap(jsonStr);
        HttpResponse response = HttpRequest.post(url)
                .addHeaders(hashMap)
                .body(jsonStr).execute();

        return response;
    }


    private HashMap<String, String> getHashMap(String str) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();

        //放access_key,不妨secrect_key注意
        stringStringHashMap.put("accessKey", accessKey);


        //加随机数参数
        String random = RandomUtil.randomNumbers(5);
//        todo 也可以抽取方法，使用rpc方式调用
        redisTemplate.opsForValue().set(REDIS_RANDOM_NAMESPACE+random,1, 10,TimeUnit.SECONDS);
        stringStringHashMap.put("random",random);


        //加时间戳
        stringStringHashMap.put("timestamp", DateTime.now().toString());
        try {
            str = URLEncoder.encode(str, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException("中文编码错误");
        }
        stringStringHashMap.put("body", str);

        //使用secrect_key签名
        stringStringHashMap.put("sign", SignUtils.genSign(str, random,secrectKey));
        return stringStringHashMap;
    }



}
